/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.MobileHandler;
import com.comut.crud.bo.NotificationHandler;
import com.comut.crud.connection.IFleetConnectionManager;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.IMobileActionLocal;
import com.comut.crud.ejb.interfaces.remote.IMobileActionRemote;
import com.comut.crud.pojo.mobile.Mobile;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import java.sql.Connection;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
public class MobileBean  implements IMobileActionLocal, IMobileActionRemote {
    private static Logger _logger = Logger.getLogger(MobileBean.class.getName());

    @PostConstruct
    private void mobilePostConstruct() {
        _logger.info("PostConstruct");
    }

    @PreDestroy
    private void mobilePreDestroy() {
        _logger.info("PreDestroy");
    }
    
    public MobileBean() {
        
    }
    
    
    public Mobile insertMobile(Mobile mobile) {        
        Connection mooveFleetConnection = MooveFleetConnectionManager.instance().getConnection();
        Connection iFleetConnection = IFleetConnectionManager.instance().getConnection();
        Mobile toRet;
        MobileHandler th;
        try{
            th = new MobileHandler(mooveFleetConnection, iFleetConnection);
            toRet = th.insertMobile(mobile);        
        }
        finally{
            MooveFleetConnectionManager.instance().freeConnection(mooveFleetConnection);
            IFleetConnectionManager.instance().freeConnection(iFleetConnection);
            th = null;
        }
        NotificationHandler.notifyAboutVehicles();
        return toRet;
    }

    //@TransactionAttribute(value=TransactionAttributeType.REQUIRES_NEW)
    public void deleteMobile(Mobile mobile) {
        if(mobile.getUid()<1)
            throw new BaseException("The UID must be a int bigger than 0", ErrorCodes.INVALID_UID);
        Connection mooveFleetConnection = MooveFleetConnectionManager.instance().getConnection();
        Connection iFleetConnection = IFleetConnectionManager.instance().getConnection();
        MobileHandler th;
        try{
            th = new MobileHandler(mooveFleetConnection, iFleetConnection);
            th.deleteMobile(mobile);        
        }
        finally{
            MooveFleetConnectionManager.instance().freeConnection(mooveFleetConnection);
            IFleetConnectionManager.instance().freeConnection(iFleetConnection);
            th = null;
        }
        NotificationHandler.notifyAboutVehicles();
    }

    public void updateMobile(String mobile) {
        Connection mooveFleetConnection = MooveFleetConnectionManager.instance().getConnection();
        Connection iFleetConnection = IFleetConnectionManager.instance().getConnection();
        MobileHandler th;
        try{
            th = new MobileHandler(mooveFleetConnection, iFleetConnection);
            th.updateMobile(mobile);        
        }
        finally{
            MooveFleetConnectionManager.instance().freeConnection(mooveFleetConnection);
            IFleetConnectionManager.instance().freeConnection(iFleetConnection);
            th = null;
        }
        NotificationHandler.notifyAboutVehicles();
    }

    public Mobile getMobile(Mobile mobile) {
        
        Connection mooveFleetConnection = MooveFleetConnectionManager.instance().getConnection();
        Connection iFleetConnection = IFleetConnectionManager.instance().getConnection();
        MobileHandler th;
        Mobile toRet;
        try{
            th = new MobileHandler(mooveFleetConnection, iFleetConnection);
            toRet = th.getMobile(mobile); 
        }
        finally{
            MooveFleetConnectionManager.instance().freeConnection(mooveFleetConnection);
            IFleetConnectionManager.instance().freeConnection(iFleetConnection);
            th = null;
        }
        return toRet;
    }
    
}
